/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.helper.IGraphElementHelper;
import com.cea.papyrus.core.helper.NodeMetaclassHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * The Class Di2UMLElementCreateCommand.
 * This class is used to create the di2 element in the diagram with its UMLElement
 */
public class DI2UMLMetaclassCreateCommand extends Di2UMLElementCreateCommand{

	ArrayList umlChildCollection = new ArrayList();
	
	ArrayList geList = new ArrayList();
	
	protected final int X_OFFSET = 20;
	
	/**
	 * The Constructor.
	 * 
	 * @param element the element
	 * @param typeDiagram 
	 * @param parent the parent
	 */
	public DI2UMLMetaclassCreateCommand(Object element, GraphElement parent,String typeDiagram) {
		super(element, parent, typeDiagram);
	}


	/**
	 * Execute.
	 */
	public void execute(){
		try{
			umlParent=LookForElement.lookForUMLElementOf(parentGE);
		}
		catch (Exception e) {
			Debug.log(e);
		}
		umlChildCollection=((NodeMetaclassHelper)elementFactory).createCollectionUMLElement(umlParent);
		Iterator iter = umlChildCollection.iterator();
		Point position = location.getCopy();
		while(iter.hasNext()){
			Element currentMetaclass = (Element)iter.next();
			this.childGe=((NodeMetaclassHelper)elementFactory).createGraphElement(currentMetaclass,parentGE,position,size);
			geList.add(childGe);
			setStereotypes(currentMetaclass);
			setPresentation(childGe);
		
			int x = position.x+((GraphNode)childGe).getSize().width+X_OFFSET;
			position= new  Point(x,position.y);
		}
	}


	/**
	 * Redo.
	 */
	public void redo(){
//		Iterator umlIter = umlChildCollection.iterator();
//		while(umlIter.hasNext()){
//			((IGraphElementHelper)elementFactory).addUMLElement((Element)umlIter.next(),umlParent);
//		}
		Iterator geIter = geList.iterator();
		while(geIter.hasNext()){
			((IGraphElementHelper)elementFactory).addGraphElement((GraphElement)geIter.next(),parentGE);
		}
	}

	/**
	 * Undo.
	 */
	public void undo(){
		//((INodeFactory)elementFactory).deleteGraphElement(graphGe);
		
		Iterator geIter = geList.iterator();
		while(geIter.hasNext()){
			((IGraphElementHelper)elementFactory).removeGraphElement((GraphElement)geIter.next(), parentGE);
		}
		
//		Iterator umlIter = umlChildCollection.iterator();
//		while(umlIter.hasNext()){
//			((IGraphElementHelper)elementFactory).removeUMLElement((Element)umlIter.next(), umlParent);
//		}

	}

	


}
